package online.siwu.memorycards

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.KeyEvent
import android.widget.SimpleAdapter
import kotlinx.android.synthetic.main.activity_statistics.*

class StatisticsActivity : AppCompatActivity() {

    private val GD = GlobalData

    private val from = arrayOf("tag", "num", "code")  //定义key的名字
    private val to = intArrayOf(R.id.tvTagTJ, R.id.tvNumTJ, R.id.tvCodeTJ)  //key所对应的控件id

    private val list: ArrayList<HashMap<String, String>> = ArrayList()
    private var map: HashMap<String, String> = HashMap()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_statistics)

        this.title = "统计:  用时(记:${GD.jiTime}秒 答:${GD.yiTime}秒)"
        btOK.setOnClickListener { gotoActivity<StartActivity>() }

        //创建map对象
        for (i in 0..(GD.exerNum - 1)) {
            val ae = GD.arrExer[i] as Int
            log(i.toString())
            map = HashMap()
            //1. ok 、err or jump
            map.put("tag", tag(i))
            //2. 第 $ 张卡片是
            map.put("num", "第${numEmp(i + 1)}张卡片是 ")
            //3. code【$code】
            map.put("code", "${GD.int2str110(ae)} 【${GD.arrCode[ae] as String}】")
            list.add(map)
        }
        lvStats.adapter = SimpleAdapter(this, list, R.layout.list_statistics, from, to)
    }

    private fun tag(i: Int): String {
        val an = GD.arrAnswer[i] as Int
        return when (an) {
            -1 -> "跳过  "
            GD.arrExer[i] -> "${GD.int2str110(an)} 正确  "
            else -> "${GD.int2str110(an)} 错误  "
        }
    }

    private fun numEmp(i: Int): String {
        return when {
            i < 10 -> "  $i  "
            i < 100 -> " $i "
            else -> i.toString()
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK)
            gotoActivity<StartActivity>()
        return false
    }
}
